<?php
/**
 * global functions
 *
 * @author tom.wang<tom.wang.sz@qq.com>
 */
 
/**
 * get all config(s)
 */
function C() {
	return ConfigUtil::getAllCfg();
}

/**
 * get app config(s)
 */
function AC($key = null) {
	return ConfigUtil::getAppCfg($key);
}

/**
 * get db config(s)
 */
function DC($key = null) {
	return ConfigUtil::getDbCfg($key);
}

/**
 * get filter config(s)
 */
function FC($key = null) {
	return ConfigUtil::getFilterCfg($key);
}

function throwException($msg, $code = 0, $class = 'CatException') {
	throw new $class($msg, $code);
}

function isPhp($version = '5.0.0') {
	static $_is_php;
	$version = (string) $version;
	if(!isset($_is_php[$version])) {
		$_is_php[$version] = version_compare(PHP_VERSION, $version) < 0 ? FALSE : TRUE;
	}
	
	return $_is_php[$version];
}

function getClientIp() {
	if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
		$onlineip = getenv('HTTP_CLIENT_IP');
	} elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
		$onlineip = getenv('HTTP_X_FORWARDED_FOR');
	} elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
		$onlineip = getenv('REMOTE_ADDR');
	} elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
		$onlineip = $_SERVER['REMOTE_ADDR'];
	}
	return preg_match("/[\d\.]{7,15}/", $onlineip, $matches) ? $matches[0] : 'unknown';
}

function stripslashesDeep($var) {
	//[2010-11-28 ecshop 添加是否为空判断]
	if(empty($var)) return $var;
	
	return is_array($var) ? array_map('stripslashesDeep', $var) : stripslashes($var);
}

function getActionName() {
	static $actionName;
	if(null === $actionName) {
		$actionName = strtolower(isset($_GET[AC('app_action_param')]) ? $_GET[AC('app_action_param')] : AC('app_default_action'));
		unset($_GET[AC('app_action_param')], $_REQUEST[AC('app_action_param')]);
	}
	
	return $actionName;
}

function getMethodName() {
	static $methodName;
	if(null === $methodName) {
		$methodName = strtolower(isset($_GET[AC('app_method_param')]) ? $_GET[AC('app_method_param')] : AC('app_default_method'));
		unset($_GET[AC('app_method_param')], $_REQUEST[AC('app_method_param')]);
	}
	
	return $methodName;
}
?>